/*
 * Copyright (c) 2015 DHC Corporation.
 */

package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import cn.com.dhc.danlu.dlware.OperatorUtils;
import cn.com.dhc.danlu.dlware.net.GeneralBusinessObserver;
import cn.com.dhc.danlu.util.ToastUtil;
import cn.com.dhc.danlu.widget.adapter.GeneralGoodsAdapter;
import cn.com.dhc.mibd.eufw.widget.android.ptr.PullToRefreshBase;
import cn.com.dhc.mibd.eufw.widget.android.ptr.PullToRefreshBase.Mode;
import cn.com.dhc.mibd.eufw.widget.android.ptr.PullToRefreshBase.OnRefreshListener2;
import cn.com.dhc.mibd.eufw.widget.android.ptr.PullToRefreshGridView;
import cn.com.dhc.mibd.eufw.widget.android.widget.AbstractGridView;
import cn.com.hd.mall.web.webservices.entity.request.merch.MerchListRequest;
import cn.com.hd.mall.web.webservices.entity.response.merch.NewMerchListResponse;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * 商品列表GridView。
 *
 * @author liyh
 * @version 1.00 2015/02/07 新建
 */
public class GoodsGridView extends AbstractGridView {
    /**
     * 请求参数类
     */
    private MerchListRequest goodsform = new MerchListRequest();
    /**
     * 是否首次获取数据。
     */
    private boolean isInit = true;
    private GeneralGoodsAdapter adapter;
    /**
     * 返回页数
     */
    private int page = 1;
    /**
     * 丹露异步加载任务类
     */
    //    private DanluTaskProxy danluTaskProxy;
    /**
     * 无分类商品的显示图片布局
     */
    private LinearLayout llytNoData;

    public GoodsGridView(Context context, PullToRefreshGridView grdGoods, LinearLayout llytNoData2) {
        super(context, grdGoods);
        this.llytNoData = llytNoData2;
    }

    public void init() {
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        ptrGridView.setMode(Mode.BOTH);
        ptrGridView.setPullLabel("", Mode.PULL_UP_TO_REFRESH);
        ptrGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                goodsform.setPage(1);
                // 下拉刷新
                getCateList(true, goodsform);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                ptrGridView.setCurrentMode(Mode.PULL_UP_TO_REFRESH);
                page++;
                goodsform.setPage(page);
                getCateList(false, goodsform);
                ptrGridView.setCurrentMode(Mode.PULL_DOWN_TO_REFRESH);
            }

        });
        //        adapter = new GeneralGoodsAdapter(getDelegate().getContext(), /*danluTaskProxy,*/ GeneralGoodsAdapter.ListType.merchlist);
        adapter = new GeneralGoodsAdapter(getContext(), /*danluTaskProxy,*/ GeneralGoodsAdapter.ListType.merchlist);
        gridView.setAdapter(adapter);
    }


    @Override
    public void onItemClickListener(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public boolean onItemLongClickListener(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    }

    /**
     * 获取商品列表。
     */
    public void getCateList(boolean isInit, MerchListRequest request) {
        hasData();

        this.isInit = isInit;
        this.goodsform = request;
        this.page = goodsform.getPage();

        // 设置为下拉刷新模式
        if (isInit) {
            ptrGridView.setMode(Mode.BOTH);
        } else {
            // FIXME: 2017/2/4 pull up to refrsh add by lyf 2017-02-04 10:05:27
        }

        // 调用获取商品列表任务
        ptrGridView.setRefreshing();  //加载动画
        agent.getApi().getMerchandisesList(goodsform).compose(OperatorUtils.<NewMerchListResponse>defaultSchedulers())
                .subscribe(new GeneralBusinessObserver<NewMerchListResponse>() {
                    @Override
                    protected void onSuccess(NewMerchListResponse newMerchListResponse) {
                        updateAdapter(newMerchListResponse);
                        ptrGridView.onRefreshComplete();
                    }

                    @Override
                    protected void onAlertTips(String displayMessage) {
                        ptrGridView.onRefreshComplete(); //停止加载动画
                        ToastUtil.getInstance().show(TextUtils.isEmpty(displayMessage) ? "商品列表获取失败！" : displayMessage);
                    }
                });
    }

    /**
     * 加载数据。
     *
     * @param resultModel 数据对象
     */
    private void updateAdapter(NewMerchListResponse resultModel) {
        if (resultModel.getMerchList() != null && resultModel.getMerchList().size() > 0) {
            hasData();
            if (isInit) {
                adapter.refreshData(resultModel.getMerchList());
            } else {
                adapter.appendData(resultModel.getMerchList());
            }
        } else {
            // 下拉刷新
            if (isInit) {
                adapter.refreshData(new ArrayList());
                //暂时空视图
                hasNoData();
            } else {
                ToastUtil.getInstance().show("没有更多商品了");
                // 禁用上拉更新
                ptrGridView.setMode(Mode.PULL_DOWN_TO_REFRESH);
            }
        }
    }

    /**
     * 没有数据时的处理。
     */
    private void hasNoData() {
        setVisibility(View.GONE);
        // 显示购物车为空时的信息
        llytNoData.setVisibility(View.VISIBLE);
    }

    /**
     * 有数据时的处理。
     */
    private void hasData() {
        setVisibility(View.VISIBLE);
        // 不显示购物车为空时的信息
        llytNoData.setVisibility(View.GONE);
    }
}